﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;

namespace DataProcessor.Process
{
    public class CmdInstructionProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CmdInstructionProcessor));


        public static void Process3071(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var info = Util.GetInfoFromMn(mn);
                if (info == null)
                {
                    Log.Warn("MN：" + mn + " 找不到对应企业！");
                    return;
                }
                var dataTime = Parser.GetDateTimeByKey("DataTime", content);
                if (dataTime == null) return;

                var gate = Parser.GetStringByKey("Gate", content);
                if (string.IsNullOrEmpty(gate)) return;
             
               
                using (var ctx = new PlatformXpfContext())
                {
                    var masterMonitor = ctx.主实时监控.Where(t => t.企业基本信息ID == info.企业基本信息ID).FirstOrDefault(t => t.企业排口ID == info.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new 主实时监控
                        {
                            企业基本信息ID = info.企业基本信息ID,
                            企业排口ID = info.ID,
                            污染物类别ID = 2
                        };
                        ctx.主实时监控.Add(masterMonitor);
                    }
                    //masterMonitor.
                }
                Log.Info("3071处理成功 " + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                Log.Error(content);
            }
        }

        public static void ProcessRemainder(string content)
        {
            //do nothing;
        }

        public static void Process3097(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var info = Util.GetInfoFromMn(mn);
                if (info == null)
                {
                    Log.Warn("MN：" + mn + " 找不到对应企业！");
                    return;
                }
                var dataTime = Parser.GetDateTimeByKey("DataTime", content);
                if (dataTime == null) return;

                var cardNo = Parser.GetStringByKey("CardNo", content);
                if (string.IsNullOrEmpty(cardNo)) return;
                var cardType = Parser.GetStringByKey("CardType", content);
                var qn = Parser.GetStringByKey("QN", content);
                if (string.IsNullOrEmpty(qn))
                {
                    qn = dataTime.Value.ToString("yyyyMMddHHmmssfff");
                }
                /*刷卡及远程充值记录 user = new 刷卡及远程充值记录();
                user.污染物编码ID = 1;
                user.充值类型ID = 101;
                user.充值日期=DateTime.Now;
                user.充值月份 = user.充值日期;
                user.卡号 = "远程控制";
                user.QN = user.充值日期.ToString("yyyyMMddHHmmssfff");
                user.充值量 = 730617;
                user.充值状态ID = 601;//601作为未发送标志，表示远程控制门禁，指令未下发；102表示下发成功；103现场门禁记录*/
                using (var contextTarget = new PlatformXpfContext())
                {
                    刷卡及远程充值记录 user = new 刷卡及远程充值记录();
                    user.污染物编码ID = 1;
                    user.充值类型ID = 101;
                    user.充值日期 = dataTime.Value;
                    user.充值月份 = user.充值日期;
                    user.卡号 = cardNo + "-" + GetCardType(cardType);
                    user.QN = qn;
                    user.充值量 = 730617;
                    user.充值状态ID = 603;//601作为未发送标志，表示远程控制门禁，指令未下发；602表示下发成功；603现场门禁记录
                    user.企业基本信息ID = info.企业基本信息ID;
                    user.企业排口ID = info.ID;
                    contextTarget.刷卡及远程充值记录.Add(user);
                    contextTarget.SaveChanges();
                }
                Log.Info("现场门禁控制(卡类型: 管理卡(admin)，运维卡(maintain))处理成功 " + content);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                Log.Error(content);
            }
        }



        private static string GetCardType(string cardType)
        {
            if (cardType.ToUpper().Contains("ADMIN") || cardType.ToUpper().Contains("管理"))
            {
                return "管理卡";
            }
            if (cardType.ToUpper().Contains("MAINTAIN") || cardType.ToUpper().Contains("运维"))
            {
                return "运维卡";
            }
            return "运维卡";

        }
    }
}
